package com.eric.r2d2.pipline;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

public class XMLFilePipeline extends FilePersistentBase implements Pipeline {
	int sfx = 0;
	String pfx = "r2d2_crawl_";

	String d = new SimpleDateFormat("d").format(new Date());
	boolean newDate = false;

	private Logger logger = LoggerFactory.getLogger(getClass());

	public XMLFilePipeline(String fileLoc) {
		this.path = fileLoc;
	}

	public void process(ResultItems resultItems, Task task) {
		if (StringUtils.isNotBlank((String) resultItems.get("results"))) {
			File dir = new File(path);
			if (!dir.exists())
				dir.mkdirs();

			// 判断是否是新的一天
			if (Integer.parseInt(new SimpleDateFormat("d").format(new Date())) > Integer.parseInt(d))
				newDate = true;
			if (newDate == true)
				sfx = 0;

			File file = null;
			long a = 500000000;
			try {
				if (new File(path + File.separator + pfx + InetAddress.getLocalHost().getHostName()
						+ new SimpleDateFormat("yyyyMMdd").format(new Date()) + "_" + sfx + ".xml").length() > a) {
					sfx += 1;
				}
				file = new File(path + File.separator + pfx + InetAddress.getLocalHost().getHostName()
						+ new SimpleDateFormat("yyyyMMdd").format(new Date()) + "_" + sfx + ".xml");

				PrintWriter printWriter = new PrintWriter(new FileWriter(file, true));
				printWriter.print(resultItems.get("results"));
				printWriter.print("\n");
				printWriter.close();
			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				logger.info(resultItems.get("url") + " Saved To File: " + file.getName());
			}
		}

	}

}
